package net.hn.hnms.biz.cockpit.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.cockpit.domain.SituationDeviceMonitor;
import net.hn.hnms.biz.cockpit.domain.SituationKeyWork;
import net.hn.hnms.biz.cockpit.domain.VideoConfig;
import net.hn.hnms.biz.cockpit.domain.vo.*;
import net.hn.hnms.biz.cockpit.mapper.ProductionSituationMapper;
import net.hn.hnms.biz.cockpit.service.IProductionManagementService;
import net.hn.hnms.biz.cockpit.service.IProductionSituationService;
import net.hn.hnms.biz.cockpit.service.ISituationDeviceMonitorService;
import net.hn.hnms.biz.cockpit.service.IVideoConfigService;
import net.hn.hnms.biz.cockpit.utils.OweDateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
public class ProductionSituationServiceImpl implements IProductionSituationService {

    private final ProductionSituationMapper productionSituationMapper;

    private final IProductionManagementService productionManagementService;

    private final IVideoConfigService videoConfigService;

    private final ISituationDeviceMonitorService situationDeviceMonitorService;


    @Override
    public ResourceReservesVo getResource(String mineCode) {
        return productionSituationMapper.getResource(mineCode);
    }

    @Override
    public ProductStatisticsVo getProductStatistics(String mineCode) {
        return productionManagementService.getProductStatistics(mineCode);
    }

    @Override
    public List<ProductDetailVo> productDetail(String mineCode) {
        return productionManagementService.productDetail(mineCode);
    }

    @Override
    public List<DrivingDetailVo> getDrivingDetail(String mineCode) {
        return productionManagementService.getDrivingDetail(mineCode);
    }

    @Override
    public List<OptionVo> getVideo(String mineCode, String tag) {
        List<VideoConfig> list = videoConfigService.list(new LambdaQueryWrapper<VideoConfig>().eq(VideoConfig::getTag, tag).eq(VideoConfig::getMineCode, mineCode));
        if (CollectionUtils.isNotEmpty(list)) {
            return list.stream().map(t -> new OptionVo(t.getCameraName(), videoConfigService.getCameraStreamUrl(t.getCameraCode()))).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public List<OptionVo> getSystems(String mineCode) {
        return productionSituationMapper.getSystems(mineCode);
    }

    @Override
    public JSONObject getDeviceMonitor(String mineCode, String sysCode) {
        JSONObject result = new JSONObject(true);
        String columns = "[{\"label\":\"设备名称\",\"name\":\"deviceName\"},{\"label\":\"运行状态\",\"name\":\"runState\"},{\"label\":\"monitorName\",\"name\":\"monitorValue\"}]";
        List<SituationDeviceMonitor> list = situationDeviceMonitorService.list(new LambdaQueryWrapper<SituationDeviceMonitor>().eq(SituationDeviceMonitor::getMineCode, mineCode).eq(SituationDeviceMonitor::getSysCode, sysCode));
        JSONArray deviceMonitorList = new JSONArray();
        if (CollectionUtils.isNotEmpty(list)) {
            SituationDeviceMonitor situationDeviceMonitor = list.get(0);
            columns = columns.replace("monitorName", situationDeviceMonitor.getMonitorPointName());
            list.forEach(d -> {
                JSONObject jsonObject = new JSONObject(true);
                jsonObject.put("id", d.getId());
                jsonObject.put("deviceName", d.getDeviceName());
                jsonObject.put("runState", d.getRunPointValue());
                jsonObject.put("monitorValue", d.getMonitorPointValue());
                deviceMonitorList.add(jsonObject);
            });
        }
        result.put("columns", JSONObject.parseArray(columns));
        result.put("list", deviceMonitorList);
        return result;
    }

    @Override
    public List<SituationKeyWork> getKeyWork(String mineCode) {
        return productionSituationMapper.getKeyWork(mineCode);
    }

    @Override
    public JSONObject getMonitorLine(String mineCode, String id) {
        SituationDeviceMonitor byId = situationDeviceMonitorService.getById(id);
        // TODO 根据id获取到的数据 取监测点的测点信息，然后查询历史曲线
        return JSONObject.parseObject("");
    }

}
